#!/bin/bash

exitRC=0
logFile=/tmp/hmc_install.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {

   # keep the log file, but ensure a different user can overwrite it next time
   chmod 666 $logFile

   exit $1$exitRC
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {

   if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]
   then
     x="$2"
# Strip version then leading directory name
     f=`echo ${x%%-[0-9]*}`
     r=`echo ${f##*/}`
     for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
     do
       if [ "$r" = "$i" ]
       then
          return
       fi
     done
   fi
     
   # Log the test install output. The format for the "normal" RPM install
   # processing inside this script is 'rpm -i <file spec> --force --nodeps'
   rpm $* 2>> $logFile
   RC=$?
   return $RC
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ]
then
   echo "Please specify directory containing installable packages"
   echo "usage: installImages  <directory>"
   exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ]
then
 echo "The directory $patchdir doesn't exist"
 echo "Please specify directory containing the installable packages."
 exit_cleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]
then
   rm -f /tmp/saved_files.tar
   cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

LANG=en_US
export LANG
r=`/opt/hsc/bin/hsc version | grep Release`
rr=`echo $r | cut -d':' -f2 | cut -d'.' -f1`
case "$rr" in
  " 2" )
   rel=2
   ;;
  * )
   exit_cleanup 6
   ;;
esac
if [ "$rel" == "2" ]
then
  if [ ! -f /opt/hsc/bin/5250proxy_420 ]
  then
     mv /opt/hsc/bin/5250proxy /opt/hsc/bin/5250proxy_420
  fi
  cp $image/5250proxy /opt/hsc/bin/
  chown bin.bin /opt/hsc/bin/5250proxy
  chmod 500 /opt/hsc/bin/5250proxy
  grep -q "MH00156: 5250proxy Fix (10-13-2004)" /opt/hsc/data/version
  if [ $? -ne 0 ]
  then
    echo "MH00156: 5250proxy Fix (10-13-2004)" >> /opt/hsc/data/version
  fi
else
  echo "---- Installation of this fix is not needed ----"
fi
exit_cleanup 0
